import axios from 'axios'
import lodash from 'lodash'
import { safe } from '.'
import { PricePolicyModel } from '~/model/PricePolicyModel'
import { PricePolicySerializer } from '~/serializer/PricePolicySerializer'
import { ContactModel, MaterialModel } from '~/model'

// 用户相关接口
export abstract class PricePolicyApi {
  public static async getAll(): Promise<PricePolicyModel[]> {
    const res = await axios.get('/price_policy/get_all/')
    const arr = await res.data as PricePolicyModel[]
    // console.log(arr);
    
    // const ret = arr.map((x) => {
    //   return {
    //     product: MaterialModel.fromObj(x.product!),
    //     consumer: lodash.assignIn(new ContactModel(), x.consumer),
    //     ...x,
    //   }
    // })
    // console.log(ret);
    return arr
  }

  public static async add(item: PricePolicyModel) {
    return safe(() => axios.post(
      'price_policy/', PricePolicySerializer.serialize(item),
    ))
  }

  public static async remove(id: number) {
    return safe(() => axios.delete(
      `price_policy/${id}/`,
    ))
  }

  public static async update(id: number, item: PricePolicyModel) {
    return safe(() => axios.put(
      `price_policy/${id}/`,
      PricePolicySerializer.serialize(item),
    ))
  }
}
